from common.utils.rights import permission_required, view_logging_required
from common.utils.file_read import file_read
from . import index_bp
from flask import render_template,request
from flask_wtf.form import CombinedMultiDict
from models.form import UploadForm
import cv2 as cv
import numpy as np


def logAdjustment(img):
    c = 255 / np.log(1 + np.max(img))
    new_image = c * (np.log(img + 1))
    new_image = np.array(new_image, dtype=np.uint8)
    print('对数动态范围调整 处理完毕')
    return new_image


# 用户增加
@index_bp.get('/log_pro')
@index_bp.post('/log_pro')
@view_logging_required
@permission_required("log:pro")
def log_pro():
    if request.method == "GET":
        return render_template('admin/cv/log.html')
    else:
        form = UploadForm(CombinedMultiDict([request.form, request.files]))
        if form.validate():
            f = request.files['file']
            file_url = file_read(f)
            img = cv.imread(file_url)
            res = logAdjustment(img)
            res_pic = "static/download/log.jpg"
            cv.imwrite(res_pic,res)
            return render_template('admin/cv/log.html', pic=file_url, pic_res=res_pic)
        else:
            return "请传入正确的文件格式"